
function htmlEscape(str) { 
    return String(str).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;') 
            .replace(/>/g, '&gt;').replace(/ /g, '&nbsp;').replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;"); 
} 
 
function htmlUnescape(value) { 
    return String(value).replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>') 
            .replace(/&amp;/g, '&').replace(/&nbsp;&nbsp;&nbsp;&nbsp;/g,'\t').replace(/&nbsp;/g, ' '); 
} 
 
/** 
 * jquery plugin codeArea 
 * @author huangyuan 
 * @date 2013 1st Aug 
 */ 
 
(function($) { 
    var codeArea = function(d, o, sender) { 
        var $t = this; 
        this.parent = $(sender); 
        this.self = null; 
        this.option = o; 
        this.data = d; 
 
        this.edit = function() { 
            $t.self.html(""); 
            $t.self.append("<div class='codeArea-edit'></div>"); 
            $t.self.find(".codeArea-edit").append( 
                    "<textarea class='codeArea-edit-textarea' rows='35' waps='off'></textarea>"); 
            $t.self.find(".codeArea-edit-textarea").val($t.data).attr("rows",$t.data.split('\n').length+4);             
        }; 
 
        this.save = function() { 
            if ($t.self.find(".codeArea-edit-textarea").length == 1) { 
                $t.data=$t.self.find(".codeArea-edit-textarea").val(); 
            } 
            $t.self.html(""); 
            // create a div to write code 
            $t.self.append("<div class='codeArea-view'></div>"); 
 
            $t.self 
                    .find(".codeArea-view") 
                    .html("<table class='codeArea-view-table'><tr><td class='codeArea-view-lineNumber'></td><td class='codeArea-view-codePart'></td></tr></table>"); 
 
            var code = $t.data.split('\n'); 
             
             
            window.countDataLine=1; 
            function addData(data){ 
                $.each(data, function(k) { 
                    if(this.length>$t.option.maxsize) 
                    { 
                        var d=new Array(); 
                        d[0]=this.slice(0,$t.option.maxsize); 
                        d[1]=this.substring($t.option.maxsize); 
                        addData(d); 
                    }else{ 
                        $t.self.find(".codeArea-view-lineNumber").append( 
                                "<span class='codeArea-view-lineNumberSpan'>" + (window.countDataLine++) + "</span>"); 
                        $t.self.find(".codeArea-view-codePart").append( 
                                "<div class='codeArea-view-codeline'>" + htmlEscape(this + " ") + "</div>"); 
                    } 
                     
                }); 
            } 
             
            addData(code); 
             
        }; 
 
        var Initial = function() { 
            $t.parent.append("<div class='codeArea'></div>"); 
            $t.self = $t.parent.find("div.codeArea:last-child"); 
            $t.save(); 
 
            return $t; 
        }; 
        return Initial(); 
    }; 
    $.fn.codeArea = function(data, o) { 
        var _default = { 
                maxsize:140//default 1 line has 140 
        }; 
        var option = $.extend(_default, o); 
        return new codeArea(data, option, this); 
    }; 
})(jQuery); 